package com.example.hello.hello;

import android.app.Application;

import com.example.hello.hello.api.ApiHttpClient;
import com.example.hello.hello.bean.User;
import com.example.hello.hello.utils.StringUtils;
import com.loopj.android.http.AsyncHttpClient;

import java.util.Properties;

/**
 * Created by Administrator on 2016/1/5.
 */
public class AppApplication extends Application {

    public static final int PAGE_SIZE = 20;// 默认分页大小

    private static AppApplication instance;
    @Override
    public void onCreate() {
        super.onCreate();
        instance = this;
        init();
        initLogin();
    }
    private void initLogin() {
        User user = getLoginUser();
        if (null != user && user.getUserid() > 0) {
            login = true;
            loginUid = user.getUserid();
        } else {
            this.cleanLoginInfo();
        }
    }
    private void init() {
        // 初始化网络请求
        AsyncHttpClient client = new AsyncHttpClient();
//        PersistentCookieStore myCookieStore = new PersistentCookieStore(this);
//        client.setCookieStore(myCookieStore);
        ApiHttpClient.setHttpClient(client);
//        ApiHttpClient.setCookie(ApiHttpClient.getCookie(this));
    }
    /**
     * 获得当前app运行的AppContext
     *
     * @return
     */
    public static AppApplication getInstance() {
        return instance;
    }

    public boolean containsProperty(String key) {
        Properties props = getProperties();
        return props.containsKey(key);
    }

    public void setProperties(Properties ps) {
        AppConfig.getAppConfig(this).set(ps);
    }

    public Properties getProperties() {
        return AppConfig.getAppConfig(this).get();
    }

    public void setProperty(String key, String value) {
        AppConfig.getAppConfig(this).set(key, value);
    }

    /**
     * 获取cookie时传AppConfig.CONF_COOKIE
     *
     * @param key
     * @return
     */
    public String getProperty(String key) {
        String res = AppConfig.getAppConfig(this).get(key);
        return res;
    }

    public void removeProperty(String... key) {
        AppConfig.getAppConfig(this).remove(key);
    }

    /**
     * 保存登录信息
     *
     * @param username
     * @param pwd
     */
    private int loginUid;
    private boolean login;

    public void saveUserInfo(final User user) {
        this.loginUid = user.getUserid();
        this.login = true;
        setProperties(new Properties() {
            {
                setProperty("user.uid", String.valueOf(user.getUserid()));
                setProperty("user.name", user.getUsername());
                setProperty("user.relname", user.getUserrealname());
                setProperty("user.gejde", String.valueOf(user.getUsergende()));
                setProperty("user.pwd",  user.getUserpassword());
                setProperty("user.phone", user.getUserphone());
                setProperty("user.img",user.getUserimg());
            }
        });
    }

    /**
     * 更新用户信息
     *
     * @param user
     */
    @SuppressWarnings("serial")
    public void updateUserInfo(final User user) {
        setProperties(new Properties() {
            {
                setProperty("user.name", user.getUsername());
                setProperty("user.relname", user.getUserrealname());
                setProperty("user.gende", String.valueOf(user.getUsergende()));
                setProperty("user.pwd",  user.getUserpassword());
                setProperty("user.phone", user.getUserphone());
                setProperty("user.img",user.getUserimg());
            }
        });
    }

    /**
     * 获得登录用户的信息
     *
     * @return
     */
    public User getLoginUser() {
        User user = new User();
        user.setUserid(StringUtils.toInt(getProperty("user.uid"), 0));
        user.setUsergende(StringUtils.toBool(getProperty("user.gende")));
        user.setUserimg(getProperty("user.img"));
        user.setUsername(getProperty("user.name"));
        user.setUserpassword(getProperty("user.pwd"));
        user.setUserphone(getProperty("user.phone"));
        user.setUserrealname(getProperty("user.relname"));
        return user;
    }

    /**
     * 清除登录信息
     */
    public void cleanLoginInfo() {
        this.loginUid = 0;
        this.login = false;
        removeProperty("user.uid", "user.gende", "user.img", "user.pwd",
                "user.phone", "user.relname");
    }

    public int getLoginUid() {
        return loginUid;
    }

    public boolean isLogin() {
        return login;
    }

    /**
     * 用户注销
     */
    public void Logout() {
        cleanLoginInfo();
        ApiHttpClient.cleanCookie();

        this.login = false;
        this.loginUid = 0;
//
//        Intent intent = new Intent(Constants.INTENT_ACTION_LOGOUT);
//        sendBroadcast(intent);
    }



}
